//1. Set directory
cd "~/Desktop/PRQ Clean Jar/"

//2. Import the raw data
use  "Garlick_StateLegislationPolicy.0.1.dta", clear

//3. Map the lexis codes into the categories that codes that are also used in Congress. (See Lowery et al 2010)

gen fpolicy = ""
//1 Agriculture
replace fpolicy = "F0400" if strpos(glcode,"G0400")

//2 Civil Rights
replace fpolicy = "F0201" if strpos(glcode,"G0201")

//3 Commerce
replace fpolicy = "F1500" if strpos(glcode,"G1500")
replace fpolicy = "F1500" if strpos(glcode,"G1501")
replace fpolicy = "F1500" if strpos(glcode,"G1502")
replace fpolicy = "F1500" if strpos(glcode,"G1503")
replace fpolicy = "F1500" if strpos(glcode,"G1504")
replace fpolicy = "F1500" if strpos(glcode,"G1510")

//4 Construction
replace fpolicy = "F1400" if strpos(glcode,"G1400")

//5 Education
replace fpolicy = "F0600" if strpos(glcode,"G0600")

//6 Energy
replace fpolicy = "F0800" if strpos(glcode,"G070")
replace fpolicy = "F0800" if strpos(glcode,"G080")

//7 Health 
replace fpolicy = "F0300" if strpos(glcode,"G0300")

//8 Legal
replace fpolicy = "F1200" if strpos(glcode,"G1200")

//9 Social Welfare
replace fpolicy = "F1300" if strpos(glcode,"G1300")

//10 Taxes
replace fpolicy = "F0208" if strpos(glcode,"G0208")

//11 Telecommunications
replace fpolicy = "F1700" if strpos(glcode,"G1700")

//12 Transportation.
replace fpolicy = "F1000" if strpos(glcode,"G1000")

//Military (this will be dropped)
replace fpolicy = "F1600" if strpos(glcode,"G1600")

//Unmatched categories (these will be dropped)
replace fpolicy = "F9999" if strpos(glcode,"G0202")
replace fpolicy = "F9999" if strpos(glcode,"G0205")
replace fpolicy = "F9999" if strpos(glcode,"G0207")
replace fpolicy = "F9999" if strpos(glcode,"G0209")
replace fpolicy = "F9999" if strpos(glcode,"G2400")
replace fpolicy = "F9999" if strpos(glcode,"G2401")
replace fpolicy = "F9999" if strpos(glcode,"G9999")

/*
Step 4: Some bills will appear multiple times within these categories (e.g. see how Commerce has a number of categories), this collapse will put each bill in those categories into a single observation
*/


gen multiples = 1
collapse (sum) multiples   , by(lex_sess lex_bill state lex_pre lex_no  year2 fpolicy)

gen allbills_st = 1
collapse (sum) allbills_st, by(year2 state fpolicy)


/*
Step 5: This .dta file contains the supporting information mentioned in the manuscript. Sources:
* Adler, E. S. and J. Wilkerson (2015). Congressional Bills Project: 1998-2014. NSF 00880066 and 00880061 1(http://www.congressionalbills.org/). 
* Garlick, A. (2017). National Policies, Agendas, and Polarization in American State Legislatures: 2011 to 2014. American Politics Research 45(6), 939–979.
* General Social Survey: Public Concern (https://gss.norc.org/get-the-data)
* Policy Agendas Project: NYT Articles (https://www.comparativeagendas.net/us)
* The Open States Project (used to identify the partisan identity of the bill sponsors in the states since ~2009)
* Contact author for do file code on constructing these variables
*/

//merge in the supporting information (bills by st
merge 1:1 year2 fpolicy state using "PRQ_laboratories_controls.0.0.dta"

/* 
Step 6: Clean up the data by dropping Military (not enough state legislative activity, and unidentified bills
*/

//drop unidentified bills
drop if fpolicy == "F9999" |  fpol == ""
//drop military 
drop if fpolicy == "F1600"

/*
Step 7: Save a version of this file, we will have to return to it later to run the state by state regressions 
*/

save "temporary.dta",replace
use "temporary.dta",clear
/*
Step 8: Make the unit of analysis the policy area by aggregating the observations.
*/

collapse (max) bills_us_all bills_us_R bills_us_D nyt gss np_score (sum) allbills_st allbills_st_R allbills_st_D, by(fpol year2 cong)

//Put the data in order
egen sessno = group(year2)
egen polid = group(fpol)
xtset polid sessno


/* 
Step 9: Create Table 4
Make sure you have installed ESTOUT to make tables
ssc install estout, replace
*/

label var gss_high "Public Concern"
label var nyt "NYT articles"
label var allbills_st "State leg. bills"
eststo clear
eststo:  reghdfe bills_us_all l(1).allbills_st nyt d.gss_high  if year2 >= 1991 & year2 <= 2015, absorb(polid sessno)
eststo:  reghdfe bills_us_all l(1).allbills_st allbills_st f(1).allbills_st nyt d.gss_high if year2 >= 1991 & year2 <= 2015, absorb(polid sessno)
esttab, se label ///
star(^ 0.1 * 0.05 ** 0.01 )   b(3) se(3)  replace ///
note("Limited GSS observations: Agriculture (2007-2015), and Science (2001-2015).")


/*
Step 10: Create Table 5
*/

//create an indicator if a policy is in the top half of nationalized policies

gen np_up = . 
replace np_up = 1 if np_score > -.02
replace np_up = 0 if np_score < -.02

label var np_up "Nationalized policy"

eststo clear
eststo:  reghdfe bills_us_all l(1).allbills_st  nyt d.gss_high  if np_up == 0 & year2 >= 1991 & year2 <= 2015 , absorb(polid sessno)
eststo:  reghdfe bills_us_all l(1).allbills_st  nyt d.gss_high  if np_up == 1 & year2 >= 1991 & year2 <= 2015 , absorb(polid sessno)
eststo:  reghdfe bills_us_all c.l(1).allbills_st##i.np_up nyt d.gss_high if year2 >= 1991 & year2 <= 2015 , absorb(polid sessno)
esttab, se label ///
star(^ 0.1 * 0.05 ** 0.01 ) mtitles("Low" "High") b(3) se(3)  replace ///
note("Low: Budget and Taxes, Education, Energy, Real Estate, Social Welfare, Transportation. High: Agriculture, Civil Rights, Commerce, Health, Legal, Telecommunications") 

/* 
Step 11: Create Table 6
*/

//Change the unit of analysis to party 
drop bills_us_all allbills_st
reshape long bills_us@ allbills_st@, i(polid nyt fpol year2) j(party) str

//clean up the sample period
drop if year2 > 2015

//indicator for Republicans
gen is_gop = 0
replace is_gop = 1 if party == "_R"

//set a number to facilitate party/policy fixed effects
egen polnoparty = group(polid is_gop)

//set the panel 
xtset polnoparty sessno

label var allbills_st  "State legislative bills"
label var bills_us "Congressional bills"

//Table 6
eststo clear
eststo: reghdfe bills_us l(1).allbills_st nyt d.gss_high  if year2 >= 2009  , absorb(polnop  )  
eststo: reghdfe bills_us l(1).allbills_st nyt d.gss_high  if is_gop == 0 & year2 >= 2009   , absorb(polnop  )  
eststo: reghdfe bills_us l(1).allbills_st nyt d.gss_high  if is_gop == 1 & year2 >= 2009  , absorb(polnop ) 
esttab, se label mtitles("all" "D" "R") ///
star(^ 0.1 * 0.05 ** 0.01) b(3) se(3) replace   

/* 
Step 12: Create Table 7
*/

use "temporary.dta",clear

//Change the unit of analysis to state party delegations in Congress (and limit sample to states with active partisan delegations for that Congress)
keep dbills_D dbills_R year2 cong fpolicy state allbills_st_D allbills_st_R nyt maxdel*  
reshape long dbills@ allbills_st@ maxdel@, i(cong fpol state year2) j(party) str

//set an indicator for republicans
gen is_gop = 0
replace is_gop = 1 if party == "_R"

//I want to drop states with no members of their state delegation in Congress, so identify how many are in each delegation
sort state party year2 fpol 
egen maxdel2 = max(maxdel), by(party year2 state)

//set an indicator for each state's party delegation
egen polnoparty = group(fpolicy state party)
xtset polnop cong

//Drop obs that are out of temporal sample covered by Open States (2009 (partial), 2011, 2013, 2015)
drop if year2 < 2009 | year > 2015

//Include all states for 2011 and 2013
gen insamp = . 
replace insamp = 1 if year >= 2011 

//Identify states that have Open States coverage for 2009 and include this biennium
local insamp "AZ CA IL LA MD MN MS NC ND NJ NV PA SD TX VT WI"
foreach i of local insamp{
replace insamp = 1 if year == 2009 & state == "`i'"
}

//set delegations to MISSING if they introduced no bills AND had no members
replace dbills = . if dbills == 0 & maxdel2 == . 
//set state delegations to not-missing if they are in the sample, even if there are no bills
replace allbills_st = 0 if insamp == 1 & allbills_st == .

label var allbills_st  "State legislative bills"
label var dbills "Congressional bills"

//state delegation
eststo clear
eststo: reghdfe dbills l(1).allbills_st   if insamp == 1, absorb(polnop ) cluster(state)
eststo: reghdfe dbills l(1).allbills_st   if is_gop == 0 & insamp == 1, absorb(polnop ) cluster(state)
eststo: reghdfe dbills l(1).allbills_st   if is_gop == 1 & insamp == 1, absorb(polnop ) cluster(state)
esttab, se label mtitles("state delegations" "dem" "gop") ///
star(^ 0.1 * 0.05 ** 0.01) b(3) se(3) replace   ///
note("States with three observations: AZ, CA, IL, LA, MD, MN, MS, NC, ND, NJ, NV, PA, SD, TX, VT, WI") 
